
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class DocWriter {

	/**
	 * 
	 */
	static final String SEPARATOR_FILE =  File.separator;
    private static final String FILE_NAME = "VN_FI_hsx_141120.doc";
    private static final String DEFAULT_FILE = System.getProperty("user.dir")+SEPARATOR_FILE+"template"+SEPARATOR_FILE+FILE_NAME;
    public String pathname;
    public HWPFDocument document;
    public POIFSFileSystem fs = null;
    public FileInputStream fis = null;
    /**
     * Constructor
     * @throws FileNotFoundException 
     * @throws IOException 
     */
    public DocWriter(String pathname) throws FileNotFoundException, IOException  {
	    this.pathname = pathname;
	    fis = new FileInputStream(DEFAULT_FILE);
	    fs = new POIFSFileSystem(fis);
	   	document = new HWPFDocument(fs);
    }


    /**
     *
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    public void replaceTxt(String originalText, String newText ) throws FileNotFoundException, IOException {
         Range range = document.getOverallRange();
         range.replaceText(originalText, newText); 
    }

    /**
     * Writes the document on disk.
     * @throws IOException 
     */
    public void writeDocument() throws IOException {
        
        	FileOutputStream fos = new FileOutputStream(pathname);
        	document.write(fos);
            fos.close();
            fis.close();
    }
}